package cn.jingzhuan.element.demo.view.activity

import android.graphics.Point
import android.graphics.Rect
import android.os.Bundle
import android.os.Handler
import android.support.v7.widget.LinearLayoutCompat
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.View
import android.view.animation.AnimationUtils
import cn.jingzhuan.element.demo.view.BaseBindingActivity
import cn.jingzhuan.element.demo.view.epoxy.controller.MusicAdapterController
import cn.jingzhuan.element.demo.view.model.AudioAlbumItem
import cn.jingzhuan.element.demo.view.model.AudioItem
import cn.jingzhuan.element.demo.view.model.ItemDescription
import cn.jingzhuan.element.demo.view.util.audio.Audio
import cn.jingzhuan.element.demo.view.util.audio.AudioPlayer
import cn.jingzhuan.element.demo.view.util.audio.Playback
import cn.jingzhuan.element.element.R
import cn.jingzhuan.element.element.databinding.ActivityMusicLayoutBinding


class MusicLayoutActivity  : BaseBindingActivity<ActivityMusicLayoutBinding>(){

    lateinit var controller : MusicAdapterController
    var isInAnimation : Boolean = false

    override fun getLayoutId(): Int = R.layout.activity_music_layout

    private var bannerList: MutableList<ItemDescription> = mutableListOf()
    private var audioList: MutableList<AudioItem> = mutableListOf()
    private var audioAlbumItemList: MutableList<AudioAlbumItem> = mutableListOf()

    init {
        var index:Int = 0
        bannerList.add(ItemDescription(MainActivity::class.java,"name:${index++}", R.mipmap.icon_tabbar_component))
        bannerList.add(ItemDescription(MainActivity::class.java,"name:${index++}", R.mipmap.icon_tabbar_component))


        audioList.add(AudioItem(index++,"Audio标题${index}","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524646852584&di=34bd7510c836eb42bbbdfe7c4374e1b9&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F0dd7912397dda144e25e4b99b9b7d0a20cf48641.jpg","",2000))
        audioList.add(AudioItem(index++,"Audio@标题${index}","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524646852584&di=34bd7510c836eb42bbbdfe7c4374e1b9&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F0dd7912397dda144e25e4b99b9b7d0a20cf48641.jpg","",2000))
        audioList.add(AudioItem(index++,"Audio@标题${index}","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524646852584&di=34bd7510c836eb42bbbdfe7c4374e1b9&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F0dd7912397dda144e25e4b99b9b7d0a20cf48641.jpg","",2000))
        audioList.add(AudioItem(index++,"Audio@标题${index}。。。长标题测试。。。。非常长。。。特别长的标题","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524646852584&di=34bd7510c836eb42bbbdfe7c4374e1b9&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F0dd7912397dda144e25e4b99b9b7d0a20cf48641.jpg","",2000))
        audioList.add(AudioItem(index++,"Audio@标题${index}。。长标题测试。。.有点长","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524646852584&di=34bd7510c836eb42bbbdfe7c4374e1b9&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F0dd7912397dda144e25e4b99b9b7d0a20cf48641.jpg","",2000))
        audioList.add(AudioItem(index++,"Audio@标题${index}","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524646852584&di=34bd7510c836eb42bbbdfe7c4374e1b9&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F0dd7912397dda144e25e4b99b9b7d0a20cf48641.jpg","",2000))

        audioAlbumItemList.add(AudioAlbumItem(index++,
                "这是专辑标题${index}",
                "这是专辑描述，专辑描述1231231231，专辑描述长度测试。。。。。。。。。",
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524653506418&di=7416d2cd01c1401be7f1156f05b45ca3&imgtype=0&src=http%3A%2F%2Fimg.25pp.com%2Fuploadfile%2Fsoft%2Fimages%2F2014%2F0327%2F20140327104316478.jpg",
                1123112,
                "分类",
                "2018-04-24",
                1,
                "适用人群，适用人群，适用人群，适用人群，适用人群，适用人群，适用人群，适用人群，",
                22))

        audioAlbumItemList.add(AudioAlbumItem(index++,
                "这是专辑标题${index}",
                "这是专辑描述，专辑描述1231231231，专辑描述长度测试。。。。。。。。。",
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524653506418&di=7416d2cd01c1401be7f1156f05b45ca3&imgtype=0&src=http%3A%2F%2Fimg.25pp.com%2Fuploadfile%2Fsoft%2Fimages%2F2014%2F0327%2F20140327104316478.jpg",
                1123112,
                "分类",
                "2018-04-24",
                1,
                "适用人群，适用人群，适用人群，适用人群，适用人群，适用人群，适用人群，适用人群，",
                22))
        audioAlbumItemList.add(AudioAlbumItem(index++,
                "这是专辑标题${index}",
                "这是专辑描述，专辑描述1231231231，专辑描述长度测试。。。。。。。。。",
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524653506418&di=7416d2cd01c1401be7f1156f05b45ca3&imgtype=0&src=http%3A%2F%2Fimg.25pp.com%2Fuploadfile%2Fsoft%2Fimages%2F2014%2F0327%2F20140327104316478.jpg",
                1123112,
                "分类",
                "2018-04-24",
                1,
                "适用人群，适用人群，适用人群，适用人群，适用人群，适用人群，适用人群，适用人群，",
                22))
        audioAlbumItemList.add(AudioAlbumItem(index++,
                "这是专辑标题${index}",
                "这是专辑描述，专辑描述1231231231，专辑描述长度测试。。。。。。。。。",
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524653506418&di=7416d2cd01c1401be7f1156f05b45ca3&imgtype=0&src=http%3A%2F%2Fimg.25pp.com%2Fuploadfile%2Fsoft%2Fimages%2F2014%2F0327%2F20140327104316478.jpg",
                1123112,
                "分类",
                "2018-04-24",
                1,
                "适用人群，适用人群，适用人群，适用人群，适用人群，适用人群，适用人群，适用人群，",
                22))
        audioAlbumItemList.add(AudioAlbumItem(index++,
                "这是专辑标题${index}",
                "这是专辑描述，专辑描述1231231231，专辑描述长度测试。。。。。。。。。",
                "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1524653506418&di=7416d2cd01c1401be7f1156f05b45ca3&imgtype=0&src=http%3A%2F%2Fimg.25pp.com%2Fuploadfile%2Fsoft%2Fimages%2F2014%2F0327%2F20140327104316478.jpg",
                1123112,
                "分类",
                "2018-04-24",
                1,
                "适用人群，适用人群，适用人群，适用人群，适用人群，适用人群，适用人群，适用人群，",
                22))


    }

    override fun onDataBind(savedInstanceState: Bundle?, binding: ActivityMusicLayoutBinding) {
        binding.viewToolbar?.title = intent.getStringExtra("title")
        setUpActionBar(binding.viewToolbar!!.toolbar)
        binding.viewToolbar?.toolbar?.inflateMenu(R.menu.menu_add)
//        binding.floatingBtn.setOnClickListener { it->
//            Toast.makeText(baseContext,"正在点击。。。",Toast.LENGTH_SHORT).show()
//            finish()
//        }

        binding.viewToolbar?.toolbar?.menu?.findItem(R.id.action_add)?.setOnMenuItemClickListener {
            true
        }


        controller = MusicAdapterController(baseContext)
        controller.setBannerList(bannerList)
        controller.setAudioList(audioList)
        controller.setAudioAlbumList(audioAlbumItemList)

        val layoutManager = LinearLayoutManager(baseContext, LinearLayoutCompat.VERTICAL, false)
        binding.recyclerView.layoutManager = layoutManager
        binding.recyclerView.adapter = controller.adapter //adapter
        binding.recyclerView.itemAnimator = null


        // 刷新事件
        binding.swipeRefreshLayout.setOnRefreshListener {
            refreshFinish()
        }

        controller.requestModelBuild()


        binding.recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {

            var _handler = Handler()
            var _delay = 300L
            var _showPlayer = Runnable{
                runOnUiThread {
                    isInAnimation = false
                    binding.audioPlayerLayout.startAnimation(AnimationUtils.loadAnimation(baseContext,R.anim.slide_in_right))
                    binding.audioPlayerLayout.visibility = View.VISIBLE
                }
            }

            override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
                // 判断上次间隔
                if (!isInAnimation){
                    isInAnimation = true
                    binding.audioPlayerLayout.startAnimation(AnimationUtils.loadAnimation(baseContext,R.anim.player_out_right))
                    binding.audioPlayerLayout.visibility = View.INVISIBLE
                    _handler.postDelayed(_showPlayer,_delay)
                } else {
                    _handler.removeCallbacks(_showPlayer)
                    _handler.postDelayed(_showPlayer,_delay)
                }
            }
        })

//        var mediaPlayer  = MediaPlayer()
//        mediaPlayer?.setVolume(100f, 100f)
//        mediaPlayer?.setWakeMode(baseContext, PowerManager.PARTIAL_WAKE_LOCK)
        var player = AudioPlayer(this)
//        player.prepare(Audio(1,"AudioTitle","http://mr1.doubanio.com/1feb971410a839ccf717a5532ddd3f2e/0/fm/song/p2235493_128k.mp4","test","test",System.currentTimeMillis()))
        player.setCompleteCallback(object: Playback.Callback {
            override fun onPrepared() {
                Log.i(TAG,"onPrepared!!!")
                player.start()
                if(player.isPlaying()!!) {
                    binding.audioPlay.isSelected = true
                }
            }

            override fun onCompelte() {
                Log.i(TAG,"onCompelte!!!")
            }

            override fun onPrepareFailed(error: String) {
                Log.i(TAG,"onPrepareFailed!!!$error")
            }

        })

        if(player.isPlaying()!!) {
            binding.audioPlay.isSelected = true
        }

        binding.audioPlay.setOnClickListener {
            if(player.isPlaying()!!) {
                player.pause() // 正在播放则暂停
                binding.audioPlay.isSelected = false  // 将按钮换成地播放按钮
            } else {
                player.resume() // 恢复播放
                binding.audioPlay.isSelected = true
            }
//            mediaPlayer?.reset()
//            mediaPlayer?.setDataSource("http://mr1.doubanio.com/1feb971410a839ccf717a5532ddd3f2e/0/fm/song/p2235493_128k.mp4")
//            mediaPlayer.setDataSource(this,Uri.parse())
//            mediaPlayer.prepareAsync()
        }


    }
    //判断一个控件是否在屏幕范围内
    private fun isInScreen(v:View):Boolean {
        var p = Point()
        windowManager.defaultDisplay.getSize(p)
        var width = p.x
        var height = p.y
        var rect = Rect(0, 0, width, height)
        if (!v.getLocalVisibleRect(rect)) {
            return false
        }
        return true
    }


    private fun refreshFinish() {
        runOnUiThread {
            binding.swipeRefreshLayout.isRefreshing = false
        }
    }


}
